package com.edol.painting.main;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;

import com.edol.painting.R;
import com.edol.painting.data.PaintingDatabase;
import com.edol.painting.utils.Constant;

import java.util.List;

/**
 * Created by Administrator on 2017/7/3.
 */

public class PaintListFragment extends Fragment implements AdapterView.OnItemClickListener {
    private PaintListAdapter mPaintAdapter;

    private GridView mPaintGridView;
    //private Context mContext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_paint_list, null);
        mPaintGridView = (GridView) view.findViewById(R.id.paint_grid_view);
        mPaintGridView.setNumColumns(Constant.PAINTING_LIST_NUM_COLUMNS);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        mPaintAdapter = new PaintListAdapter(getActivity());
        mPaintGridView.setAdapter(mPaintAdapter);
        mPaintGridView.setOnItemClickListener(this);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();

    }

    public void setData(List<PaintingDatabase.PaintingEntry> data) {
        if (mPaintAdapter != null) {
            mPaintAdapter.setData(data);
        }
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        //enter paints preview
        if (mPaintAdapter != null) {
            PaintController.getInstances().enterPaintPreviewPage(getActivity(), mPaintAdapter.getData(), position);
        }
    }

}
